package ThreadState;

/**
 * 推荐使用标志位让线程停止
 */
public class ThreadStop implements Runnable{

    private boolean flag = true;

    public void stop(){
        this.flag = false;
    }

    public static void main(String[] args) {
        ThreadStop threadStop = new ThreadStop();

        new Thread(threadStop).start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("main-->"+i);

            if(i == 900){
                threadStop.stop();
                break;
            }
        }
    }

    @Override
    public void run() {
        int i = 0;
        while(flag)
            System.out.println("线程正在运行-->"+i++);
    }
}
